var params = new Object();
var tableNames;
var fileId;
var attachmentParamterId = "9070" ;
$(function () {
    //获取父页面的值
    setValue()
});

//获取父页面的值
function setValue() {
    var name, value;
    var str = decodeURI(decodeURI(window.location.href));
    var num = str.indexOf("?")
    str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
    // 设值
    var arr = str.split("&"); //各个参数放到数组里
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substring(0, num);
            value = arr[i].substr(num + 1);
            if ("tableNames" == name) {
                tableNames = value;
            }
        }
    }
}

//导入弹窗确定按钮单击事件处理
function btnGetExcelSheet() {

    if (!checkExcel()) {
        return;
    }
    params.attachmentParamterId =attachmentParamterId;
    $.ajaxFileUpload({
        url: '/sys-web/common/excelHandleIndex/saveExcelToService',
        secureuri: false,
        fileElementId: 'file',
        data: params,
        dataType: 'text',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.code != '0000') {
                $.messager.alert("提示", data.msg, "error", function () {
                });
            } else {
                fileId = data.returnObject;
                var filename = $("#file").val();
                var index = filename.lastIndexOf("\\");
                filename = filename.substr(index +1);
                var url = '/sys-web/common/excelHandleIndex/excelSheetIndex?filename=' + filename + '&tableNames=' + tableNames + '&fileId=' + fileId +'&attachmentParamterId='+attachmentParamterId;
                layer.open({
                    type: 2,
                    title: '',
                    shadeClose: false,
                    shade: 0.3,
                    area: ['420px', '480px'],
                    content: url,
                    maxmin: false
                });
            }

        },
        error: function (data) {
            $.messager.alert("提示", "上传失败", "info", function () {
            });
        }
    });

}

//检查Excel参数是否合法
function checkExcel() {
    if ($("#file").val() == "" || $("#file").val() == "undefined") {
        $.alert("default", "info", "请选择文件");
        return false;
    }
    var bcheck = false;
    var filename = $("#file").val();
    var flag = false; //状态
    var arr = ["xls", "xlsx","csv"];
    //取出上传文件的扩展名
    var index = filename.lastIndexOf(".");
    var ext = filename.substr(index + 1);
    //循环比较
    for (var i = 0; i < arr.length; i++) {
        if (ext == arr[i]) {
            bcheck = true; //
            break;
        }
    }
    if (!bcheck) {
        $.alert("default", "info", "文件类型不正确");
        return false;
    }
    params.tableNames = tableNames;

    return true;
}

/**
 * 取消
 */
function btnReset() {
    $("#file").val("");
    closeLayer();
}
